const db = require('../db/index')
const {baseURL} = require('../config')
//获取宠物列表接口
exports.getPetInfo = (req, res) => {
  const sql = `SELECT  * FROM t_pet_note`
  db.query(sql,(err, results) => {
    // 1.执行 SQL 语句失败
      if(err) return res.resMessage(err)
      //2. 执行SQL语句成功
      results.forEach(item => {
        item.pet_avatar = baseURL + item.pet_avatar
      });
      res.send({
        status: 0,
        message: '获取宠物列表成功',
        data: results
      })
  })
}

// 获取发布数量
exports.getPublishSum = (req, res) =>{
  const sql = `SELECT COUNT(id) as sum FROM t_pet_note`
  db.query(sql,(err, results) => {
      if(err) return res.resMessage(err)
     res.send({
      status: 0,
      message:'获取发布量成功',
      data: results[0].sum
     })
  })
}

//申请领养宠物
exports.getApplyUser = (req, res) => {
  const sql =  `insert into t_apply set ? `
  console.log(req.body)
  db.query(sql, {pet_id: req.body.pet_id, user_id:req.user.user_id},function(err, results){
    if(err)return res.resMessage(err)
    res.send({
      status: 0,
      message: '申请成功'
    })
  })
}

// 获取发布人的昵称和头像